<template>
   <div class="page-wrapper">
    <PageTools>
      <template #after>
        <el-button type="primary" size="small" @click="ShowaddPmission(1,'0')">新增</el-button>
      </template>
    </PageTools>
    <el-card>
      <el-table :data="list" border row-key="id" default-expand-all>
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作" >
        <template v-slot="scope">
          <!-- 只有第一层才能新增，其他不行 -->
            <el-button type="text" v-if="scope.row.type===1" @click="ShowaddPmission(2,scope.row.id)">新增</el-button>
            <el-button type="text" @click="showEditDialog(scope.row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(scope.row.id)">删除</el-button>
          </template>
          </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="编辑权限" :visible="isShowDialog" @close="btnCancel">
    
      <el-form ref="addForm" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input  v-model="formData.name"/>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input  v-model="formData.code"/>
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input  v-model="formData.description"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" type="primary" @click="btnOK" :disabled="loading">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </template>
    </el-dialog>
  </div>
  </template>
  
  <script>
  import {getPermissionList, getPermissionDetail,updatePermission,addPermission,deletePermission} from '@/api/permission'
  import {listToTree} from '@/utils'
  export default {
  
    data(){
      return{
        loading:false,
        list: [],
        isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: ''
      },
    
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
    created(){
      this.loadPermissionList()
    },
    methods:{
      async loadPermissionList(){

        // const res=await getPermissionList()
        // console.log(res)
        // this.list=await getPermissionList()
        // console.log(this.list)
        const res =await getPermissionList()
        this.list=listToTree(res,'0')
        console.log('转换后的新数据',this.list)
      },      
            // 弹窗添加
         
              async showEditDialog(id) {
      console.log('作用域插槽获取到的数据', id)
      this.formData = await getPermissionDetail(id)
      this.isShowDialog = true
    },

    //编辑
    async btnOK() {
      // 等待校验
      await this.$refs.addForm.validate()
      this.loading=true
      // 判断是添加还是编辑
      if(this.formData.id){
   // 发请求
      await updatePermission(this.formData)
      }else{
        await addPermission(this.formData)
      }
   
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowDialog = false
      // 更新页面
      this.loadPermissionList()
      this.loading=false
    
    },

    btnCancel() {
      // 清理报错
      this.$refs.addForm.resetFields()
      // 清理数据
      this.formData = {
        name: '',
        code: '',
        description: ''
      }

      // 关闭弹窗
      this.isShowDialog = false
    },


    // 添加
    ShowaddPmission(type,pid){
      this.formData.type=type
      this.formData.pid=pid
      this.isShowDialog=true
    },

    // 删除
   async delPermission(id){
    try {
         await this.$confirm('是否确认删除')
   await deletePermission(id)
  //  提示弹框
  this.$message.success('删除成功')
    this.loadPermissionList()
    } catch (error) {
      console.log(error)
    }
 
    }
  }
}


  </script>
  
  <style>
  
  </style>